import Vue from 'vue';
import Router from 'vue-router';


import noFound from '@/pages/common/404';
import loginPage from '@/pages/common/login.vue';
import main from '@/pages/main';

Vue.use(Router);
const originalPush = Router.prototype.push;
Router.prototype.push = function push(location) {
  return originalPush.call(this, location).catch((err) => err);
};
// 全局路由(无需嵌套在main                         里面)
const globalRoutes = [
  {
    name: '对话',
    path: '/',
    component: main,
  },
  {
    name: '登录',
    path: '/login',
    component: loginPage,
  },
  {
    name: '',
    path: '*',
    component: noFound,
  },
];
// main主入口路由(子路由嵌套在main里面)
const mainRoutes = {
  name: '主页',
  path: '/main',
  component: main,
  redirect: '',
  children: [],
};

// // 获取菜单数据，设置动态路由
function getMainRoutes() {
  let menuTreeData = [{
    applicationId: 1,
    children: [],
    label: '对话',
    name: 'chat',
    parentId: '99',
    permissionId: '99.1',
    permissionType: '1',
  }, {
    applicationId: 2,
    children: [],
    label: '上传知识库',
    name: 'upload',
    parentId: '99',
    permissionId: '99.2',
    permissionType: '2',
  }, {
    applicationId: 3,
    children: [],
    label: '智能体',
    name: 'intelligent',
    parentId: '99',
    permissionId: '99.3',
    permissionType: '3',
    }, 
    {
      applicationId: 4,
      children: [],
      label: '用户管理',
      name: 'role',
      parentId: '199',
      permissionId: '99.4',
      permissionType: '4',
    }, 
    {
      applicationId: 5,
      children: [],
      label: '问答审核',
      name: 'answer',
      parentId: '299',
      permissionId: '99.5',
      permissionType: '45',
    },
    {
      applicationId: 6,
      children: [],
      label: '人工纠错',
      name: 'errorlist',
      parentId: '399',
      permissionId: '99.6',
      permissionType: '46',
    },
    {
      applicationId: 7,
      children: [],
      label: '敏感词汇',
      name: 'filter',
      parentId: '599',
      permissionId: '99.7',
      permissionType: '47',
    },
    {
      applicationId: 8,
      children: [],
      label: '专业知识系统',
      name: 'knowledge',
      parentId: '699',
      permissionId: '99.8',
      permissionType: '48',
    },
  ]; //为子系统添加虚假路由
  let mainRoutesRedirect = '';
  let mainRoutesChildren = [];
  menuTreeData.forEach((item, index) => {
    if (item.children && item.children.length > 0) {
      // 有2级菜单
      item.children.forEach((item2, indexs) => {
        if (item2.children && item2.children.length > 0) {
          // 有3级菜单
          menuTreeData[index].children[indexs].showChildren = true;
          item2.children.forEach((item3, ind) => {
            if (indexs === 0 && ind === 0) {
              mainRoutesRedirect = '/main/' + item3.name;
            }
            let obj = {
              name: item3.label,
              path: item3.name,
              component: () => import(`@/pages/${item.name}/${item2.name}/${item3.name}`), // 这里非常关键，要使用es6拼接，才能被webpack编译
              meta: {
                isTab: true,
                id: item3.id,
                keepAlive: true, // 默认缓存
              },
            };
            // if (loadNoKeepAlive.indexOf(item2.name) > -1) {
            //   obj.meta.keepAlive = false
            // }
            mainRoutesChildren.push(obj);
          });
        } else {
          if (index === 0 && indexs === 0) {
            mainRoutesRedirect = '/main/' + item2.name;
          }
          let obj = {
            name: item2.label,
            path: item2.name,
            component: () => import(`@/pages/${item.name}/${item2.name}`), // 这里非常关键，要使用es6拼接，才能被webpack编译
            meta: {
              isTab: true,
              id: item2.id,
              keepAlive: true, // 默认缓存
            },
          };
          // if (loadNoKeepAlive.indexOf(item2.name) > -1) {
          //   obj.meta.keepAlive = false
          // }
          mainRoutesChildren.push(obj);
        }
      });
    } else {
      // 只有1级菜单
      mainRoutesRedirect = '/main/' + item.name;
      let obj = {
        name: item.label,
        path: item.name,
        component: () => import(`@/pages/common/${item.name}`), // 这里非常关键，要使用es6拼接，才能被webpack编译
        meta: {
          isTab: true,
          id: item.id,
          keepAlive: true, // 默认缓存
        },
      };
      mainRoutesChildren.push(obj);
    }
  });
  mainRoutes.redirect = mainRoutesRedirect; // main的重定向
  mainRoutes.children = mainRoutesChildren; // mian的子路由
  // console.log(mainRoutes.children)
  return true;
}

const router = new Router({
  routes: globalRoutes.concat(mainRoutes),
});

window.flag = 0;
router.beforeEach((to, from, next) => {
  // console.log(window.flag)
  if (to.path !== '/') {
    if (window.flag === 0 && getMainRoutes()) {
      router.addRoutes([mainRoutes]);
      window.flag++;
      next({ ...to, replace: true });
    } else {
      next();
    }
  } else {
    next({ path: '/login', replace: true });
  }
});

export default router;
